package repertory.android.ericchen.androidrepertory.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import repertory.android.ericchen.androidrepertory.R;
import repertory.android.ericchen.androidrepertory.database.MyORMLiteDataBaseHelper;
import repertory.android.ericchen.androidrepertory.database.MySQLiteDataBaseHelper;

/**
 * className:DataBaseActivity
 * author: Ericchen
 * description:
 * date: 2017/3/6 14:16
 * update: 2017/3/6
 * version:
 *
 * 数据库操作
 */
public class DataBaseActivity extends Activity implements View.OnClickListener{
    private Button BtnAdd;
    private Button BtnDel;
    private Button BtnUpdate;
    private Button BtnCheck;
    private TextView dataText;

    private MySQLiteDataBaseHelper mMyDatabaseHelper;
    private SQLiteDatabase database;
    private MyORMLiteDataBaseHelper ormLiteDataBaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_database);
        initView();
        mMyDatabaseHelper = new MySQLiteDataBaseHelper(getApplicationContext());
        database = mMyDatabaseHelper.getReadableDatabase();
        ormLiteDataBaseHelper = MyORMLiteDataBaseHelper.getHelper(getApplicationContext());
    }
    private void initView(){
        BtnAdd =(Button)findViewById(R.id.btn_add);
        BtnAdd.setOnClickListener(this);
        BtnDel =(Button)findViewById(R.id.btn_delete);
        BtnDel.setOnClickListener(this);
        BtnUpdate =(Button)findViewById(R.id.btn_update);
        BtnUpdate.setOnClickListener(this);
        BtnCheck =(Button)findViewById(R.id.btn_check);
        BtnCheck.setOnClickListener(this);
        dataText =(TextView)findViewById(R.id.data_text);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add:
                insert();
                ++count;
                break;
            case R.id.btn_delete:
                --count;
                delete();
                break;
            case R.id.btn_update:
                update();
                break;
            case R.id.btn_check:
                check();
                break;
        }
    }
    private int count =1;
    private void insert(){
        ContentValues values = new ContentValues();

        values.put("username", "test1");
        values.put("count", count);

        //向数据库插入数据
        long result = database.insert("User", null, values);
        Toast.makeText(DataBaseActivity.this, "插入成功", Toast.LENGTH_SHORT).show();
//        UserBean bean = new UserBean("test",count);
//        try{
//            ormLiteDataBaseHelper.getUserBean().create(bean);
//        }catch (SQLException e){
//            e.printStackTrace();
//        }
    }
    private void delete(){
        //删除数据库的数据
        int result = database.delete("User", "count=?", new String[]{count + ""});
        if(result>0){
            Toast.makeText(DataBaseActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(DataBaseActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
        }
//        try{
//            ormLiteDataBaseHelper.getUserBean().deleteById(1);
//        }catch (SQLException e){
//            e.printStackTrace();
//        }

    }
    private void update(){
        ContentValues values = new ContentValues();
        values.put("username", "test2");
        //更新数据库的数据
        int result = database.update("User", values, "count=?", new String[]{"1"});
        if(result>0){
            Toast.makeText(DataBaseActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(DataBaseActivity.this, "更新失败", Toast.LENGTH_SHORT).show();
        }
//        UserBean bean = new UserBean("test2",count);
//        bean.setId(1);
//        try{
//            ormLiteDataBaseHelper.getUserBean().update(bean);
//        }catch (SQLException e){
//            e.printStackTrace();
//        }
    }
    private void check(){
        StringBuffer read = new StringBuffer();
        //对数据库表进行查询，会返回游标
        Cursor cursor = database.query("User", null, null, null, null, null, null);

        while (cursor.moveToNext()){
            String username = cursor.getString(cursor.getColumnIndex("username"));
            int count = cursor.getInt(cursor.getColumnIndex("count"));
            read.append("username="+username+",count="+count+"\n");
        }
        dataText.setText("数据库数据:"+read.toString());
        cursor.close();
//        try{
//            List<UserBean> list = ormLiteDataBaseHelper.getUserBean().queryForAll();
//            StringBuffer read = new StringBuffer();
//            for(UserBean temp:list){
//                read.append("username="+temp.getUsername()+",count="+temp.getCount()+"\n");
//            }
//            dataText.setText("数据库数据:"+read.toString());
//        }catch (SQLException e){
//            e.printStackTrace();
//        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ormLiteDataBaseHelper.close();
    }
}
